package ParseJson2Bean;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 名称: StringUtils.java <br>
 * 描述: <br>
 * 最近修改时间:2018/6/26 17:26 <br>
 *
 * @version [版本号, V1.0]
 * @since 2018/6/26 17:26
 * @author zhipingzhang  
 */
public class StringUtils {
    /**
     * 将string按需要格式化,前面加缩进符,后面加换行符
     * @param tabNum 缩进量
     * @param srcString
     * @return
     */
    public static String formatSingleLine(int tabNum, String srcString) {
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<tabNum; i++) {
            sb.append("\t");
        }
        sb.append(srcString);
        sb.append("\n");
        return sb.toString();
    }

    public static String firstToUpperCase(String key) {
        return key.substring(0, 1).toUpperCase(Locale.CHINA) + key.substring(1);
    }

    public static String gapToCamel(String src) {
        StringBuilder sb = new StringBuilder();
        for(String s : src.trim().split(" ")) {
            sb.append(firstToUpperCase(s));
        }
        return sb.toString();
    }

    /**
     * 驼峰转下划线命名
     */
    public static String camelTo_(String src) {
        StringBuilder sb = new StringBuilder();
        StringBuilder sbWord = new StringBuilder();
        char[] chars = src.trim().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if(c >= 'A' && c <= 'Z') {
                // 一旦遇到大写单词，保存之前已有字符组成的单词
                if(sbWord.length() > 0) {
                    if(sb.length() > 0) {
                        sb.append("_");
                    }
                    sb.append(sbWord.toString());
                }
                sbWord = new StringBuilder();
            }
            sbWord.append(c);
        }

        if(sbWord.length() > 0) {
            if(sb.length() > 0) {
                sb.append("_");
            }
            sb.append(sbWord.toString());
        }

        return sb.toString();
    }

    public static boolean hasChinese(String s) {
        String regexChinese = "[\u4e00-\u9fa5]+";
        Pattern patternChinese = Pattern.compile(regexChinese);
        return patternChinese.matcher(s).find();
    }

    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0;
    }

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String camel(String str) {
        //利用正则删除下划线，把下划线后一位改成大写
        Pattern pattern = Pattern.compile("_(\\w)");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if(matcher.find()) {
            sb = new StringBuffer();
            //将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
            //把之后的也添加到StringBuffer对象里
            matcher.appendTail(sb);
        }else {
            return sb.toString();
        }
        return camel(sb.toString());
    }

}
